package com.example.healkep;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.healkep.utils.GetWeek;
import com.example.healkep.utils.InformationUtils;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ChangeInformActivity extends AppCompatActivity {
    private static String sessionid = LoginActivity.getSessionID();
    private int year;
    private int month;
    private int day;

    public static String getSessionid() {
        return sessionid;
    }

    public static void setSessionid(String sessionid) {
        ChangeInformActivity.sessionid = sessionid;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_inform);

        Spinner birth = findViewById(R.id.cbirth);
        EditText height = findViewById(R.id.cheight1);
        EditText weight1 = findViewById(R.id.cweight1);
        EditText weight2 = findViewById(R.id.cweight2);
        Spinner year = findViewById(R.id.cyear);
        Spinner month = findViewById(R.id.cmonth);
        Spinner day = findViewById(R.id.cday);
        Spinner num = findViewById(R.id.cnum);
        Button updateinform = findViewById(R.id.cupdate);

        String[] births = new String[101];
        int[] tempbirths = new int[101];
        for (int i = 0; i <= 100; i++) {
            tempbirths[i] = 1950 + i;
            births[i] = Integer.toString(tempbirths[i]);
        }
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, births);
        birth.setAdapter(adapter1);

        /*String[] heights = getResources().getStringArray(R.array.height);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, heights);
        height.setAdapter(adapter2);

        String[] weights = getResources().getStringArray(R.array.weight);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, weights);
        weight1.setAdapter(adapter3);
        weight2.setAdapter(adapter3);*/

        String[] years = new String[100];
        int[] tempyears = new int[100];
        for (int i = 0; i < 100; i++) {
            tempyears[i] = i + 2020;
            years[i] = Integer.toString(tempyears[i]);
        }
        ArrayAdapter<String> adapter4 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, years);
        year.setAdapter(adapter4);

        String[] months = new String[12];
        int[] tempmonths = new int[12];
        for (int i = 0; i < 12; i++) {
            tempmonths[i] = i + 1;
            months[i] = Integer.toString(tempmonths[i]);
        }
        ArrayAdapter<String> adapter6 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, months);
        month.setAdapter(adapter6);

        String[] days = new String[31];
        int[] tempdays = new int[31];
        for (int i = 0; i < 31; i++) {
            tempdays[i] = i + 1;
            days[i] = Integer.toString(tempdays[i]);
        }
        ArrayAdapter<String> adapter7 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, days);
        day.setAdapter(adapter7);

        String[] nums = getResources().getStringArray(R.array.nums);
        ArrayAdapter<String> adapter5 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, nums);
        num.setAdapter(adapter5);

        updateinform.setOnClickListener(v -> {
            String username = LoginActivity.getUser();
            int age = 2022 - Integer.parseInt(birth.getSelectedItem().toString());
            int h = Integer.parseInt(height.getText().toString());
            int w1 = Integer.parseInt(weight1.getText().toString());
            double bmi = 1.0 * w1 / (((1.0 * h) / 100) * ((1.0 * h) / 100));
            String energy = null;
            String aveenergy = null;
            String enweight = null;
            String lenweight = null;
            if (bmi < 18.5) {
                if (num.getSelectedItem().toString().equals("单胎")) {
                    enweight = "13.0~18.5";
                    lenweight = "0.51(0.44~0.58)";
                } else if (num.getAdapter().toString().equals("双胞胎")) {
                    enweight = null;
                    lenweight = null;
                }
            } else if (bmi >= 18.5 && bmi < 25) {
                if (num.getSelectedItem().toString().equals("单胎")) {
                    enweight = "11.5~16";
                    lenweight = "0.42(0.35~0.50)";
                } else if (num.getAdapter().toString().equals("双胞胎")) {
                    enweight = "17~25";
                    lenweight = null;
                }
            } else if (bmi >= 25 && bmi < 30) {
                if (num.getSelectedItem().toString().equals("单胎")) {
                    enweight = "7~11.5";
                    lenweight = "0.28(0.23~0.3)";
                } else if (num.getAdapter().toString().equals("双胞胎")) {
                    enweight = "14~23";
                    lenweight = null;
                }
            } else if (bmi >= 30) {
                if (num.getSelectedItem().toString().equals("单胎")) {
                    enweight = "5~9";
                    lenweight = "0.22(0.17~0.27)";
                } else if (num.getAdapter().toString().equals("双胞胎")) {
                    enweight = "11~19";
                    lenweight = null;
                }
            }
            setYear(Integer.parseInt(year.getSelectedItem().toString()));
            setMonth(Integer.parseInt(month.getSelectedItem().toString()));
            setDay(Integer.parseInt(day.getSelectedItem().toString()));
            String date = "" + getYear() + "-" + getMonth() + "-" + getMonth();
            String json = "{\n" +
                    "    \"username\":\"" + username + "\",\n" +
                    "    \"birth\":\"" + birth.getSelectedItem().toString() + "\",\n" +
                    "    \"height1\":\"" + height.getText().toString() + "\",\n" +
                    "    \"weight1\":\"" + weight1.getText().toString() + "\",\n" +
                    "    \"date\":\"" + date + "\",\n" +
                    "    \"num\":\"" + num.getSelectedItem().toString() + "\",\n" +
                    "    \"age\":\"" + age + "\",\n" +
                    "    \"energy\":\"" + energy + "\",\n" +
                    "    \"aveenergy\":\"" + aveenergy + "\",\n" +
                    "    \"enweight\":\"" + enweight + "\",\n" +
                    "    \"lenweight\":\"" + lenweight + "\"\n" +
                    "}";
            long week = GetWeek.week(date);
            String json2 = "{\n" +
                    "    \"id\":\"" + username + "\",\n" +
                    "    \"weight\":\"" + weight2.getText().toString() + "\",\n" +
                    "    \"week\":\"" + week + "\"\n" +
                    "}";
            new Thread(() -> {
                try {
                    OkHttpClient clientupdate = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(LoginActivity.url + "/information/update")
                            .addHeader("Cookie", sessionid)
                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                            .build();
                    clientupdate.newCall(request).execute();
                    OkHttpClient clientupdate2 = new OkHttpClient();
                    Request request2 = new Request.Builder()
                            .url(LoginActivity.url + "/weight/update")
                            .addHeader("Cookie", sessionid)
                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                            .build();
                    clientupdate2.newCall(request2).execute();
                } catch (Exception e) {
                    runOnUiThread(() -> Toast.makeText(ChangeInformActivity.this, "修改异常!!", Toast.LENGTH_SHORT).show());
                }
                finish();
            }).start();
        });
    }
}